package com.example.module_lzq_haibathree.activity

import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.module_lzq_haibathree.databinding.ActivitySpiritLevelBinding



/**
 * =====================================================
 * 作    者: Xiuz
 * 创建日期: 2020/7/30 14:58
 * 描    述:
 * =====================================================
 */
class SpiritLevelActivity : AppCompatActivity() {
    private lateinit var binding: ActivitySpiritLevelBinding
    private lateinit var mSensorManager: SensorManager
    private lateinit var mSensorEventListener: SensorEventListener


    private var values: FloatArray? = null;
    private var r: FloatArray? = null;
    private var gravity: FloatArray? = null;
    private var geomagnetic: FloatArray? = null;
    private var manager: SensorManager? = null

    private var magneticSensor: Sensor? = null;
    private var  accelerometerSensor:Sensor? = null;


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySpiritLevelBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.toolbar.setNavigationOnClickListener {
            finish()
        }


        //获取SensorManager
        manager = getSystemService(SENSOR_SERVICE) as SensorManager
        //获取Sensor
        //获取Sensor
        magneticSensor = manager!!.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD)
        accelerometerSensor = manager!!.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
        //初始化数组
        //初始化数组
        values = FloatArray(3) //用来保存最终的结果

        gravity = FloatArray(3) //用来保存加速度传感器的值

        r = FloatArray(9) //

        geomagnetic = FloatArray(3) //用来保存地磁传感器的值




        mSensorManager = getSystemService(SENSOR_SERVICE) as SensorManager

        mSensorEventListener = object : SensorEventListener {
           /* override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
            }

            override fun onSensorChanged(event: SensorEvent) {
                binding.spiritLevelView.post { binding.spiritLevelView.updataView(event.values) }

            }*/

            override fun onSensorChanged(event: SensorEvent) {
                if (event.sensor.type == Sensor.TYPE_MAGNETIC_FIELD) {
                    geomagnetic = event.values
                }
                if (event.sensor.type == Sensor.TYPE_ACCELEROMETER) {
                    gravity = event.values
                    getValue()
                }
            }

            override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {}
        }

       /* mSensorManager.registerListener(
            mSensorEventListener, mSensorManager.getDefaultSensor(
                Sensor.TYPE_ORIENTATION
            ),
            SensorManager.SENSOR_DELAY_GAME
        )*/
    }

    override fun onDestroy() {
        super.onDestroy()
        mSensorManager.unregisterListener(mSensorEventListener)
    }


    override fun onResume() {
        super.onResume()
        manager!!.registerListener(mSensorEventListener, magneticSensor, SensorManager.SENSOR_DELAY_NORMAL)
        manager!!.registerListener(mSensorEventListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL)
//        AdUtil.showBannerAd(this,binding.adFrameLayout)
    }

    fun getValue() {
        // r从这里返回
        SensorManager.getRotationMatrix(r, null, gravity, geomagnetic)
        //values从这里返回
        SensorManager.getOrientation(r, values)
        //提取数据
        var azimuth = Math.toDegrees(values!![0].toDouble())
        if (azimuth < 0) {
            azimuth = azimuth + 360
        }
        val pitch = Math.toDegrees(values!![1].toDouble())
        val roll = Math.toDegrees(values!![2].toDouble())

        binding.spiritLevelView.invalidate()

        binding.spiritLevelView.post { binding.spiritLevelView.updataView(pitch,roll) }
    }
}
